using System;
using System.IO;
using UnityGameFramework.Runtime;

namespace LS.Saving
{
    public class DefaultDataFile: IDataFile
    {
        private readonly string m_FileName;
        private readonly string m_Extension;
        private readonly string m_Directory;
        public DateTime Timestamp
        {
            get
            {
                return SavingIO.GetUniversalTimestamp(FullPath);
            }
        }
        
        public string FileName
        {
            get
            {
                return m_FileName;
            }
        }

        public string Extension
        {
            get
            {
                return m_Extension;
            }
        }

        public string Directory
        {
            get
            {
                return m_Directory;
            }
        }

        public string FullPath
        {
            get
            {
                return string.Format("{0}/{1}.{2}", Directory, FileName, Extension).Replace("\\", "/");
            }
        }

        public bool Existed
        {
            get
            {
                return SavingIO.FileExists(FullPath);
            }
        }

        public DefaultDataFile(string directory, string fileName, string extension)
        {
            if (extension.StartsWith("."))
            {
                extension = extension.Remove(0, 1);
            }

            this.m_Directory = directory;
            this.m_FileName = fileName;
            this.m_Extension = extension;
        }

        public DefaultDataFile(string fullPath)
        {
            this.m_Directory = Path.GetDirectoryName(fullPath);
            this.m_FileName = Path.GetFileNameWithoutExtension(fullPath);
            this.m_Extension = Path.GetExtension(fullPath);
        }
        
        public DefaultDataFile(DefaultDataFile file)
        {
            this.m_Directory = file.Directory;
            this.m_FileName = file.FileName;
            this.m_Extension = file.Extension;
        }

        public override int GetHashCode()
        {
            return FullPath.GetHashCode();
        }
    }
}
